<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\Inventory;

class ProductController extends Controller
{
	public function actionIndex(){
		$query = new \yii\db\Query();

		$displayAll = Yii::$app->request->get("displayAll",0);
		if ($displayAll == 1){
			$list = $query->select("*")->from("JXC_PRODUCT")->orderBy('PRODUCTID DESC')->all();
		}else{
			$list = $query->select("*")->from("JXC_PRODUCT")->orderBy('PRODUCTID DESC')->limit(DEFAULT_LIMIT)->all();
		}

		$sumCostprice = 0;
		$sumSellingprice = 0;
		foreach ($list as  $value) {
			$sumCostprice += $value['COSTPRICE'];
			$sumSellingprice += $value['SELLINGPRICE'];
		}
		return $this->render("index",['list' => $list,'sumCostprice'=>$sumCostprice,'sumSellingprice'=>$sumSellingprice]);
	}

	private function generateQueryString($fieldName){
		if (empty($fieldName))
			return "";
		$getFieldNameRequest = $_GET[$fieldName];
		if (empty($getFieldNameRequest))
			return "";
		else
			return $fieldName." like '%".$getFieldNameRequest."%'";
	}

	public function actionSearch()
	{
		$query = new \yii\db\Query();

		$query->select("*")
				->from("JXC_PRODUCT");

		$querySubString = $this->generateQueryString("productname");
		if (!empty($querySubString)){
			$query->andWhere($querySubString);
		}

		$querySubString = $this->generateQueryString("drawingno");
		if (!empty($querySubString)){
			$query->andWhere($querySubString);
		}

		$querySubString = $this->generateQueryString("specification");
		if (!empty($querySubString)){
			$query->andWhere($querySubString);
		}

		$querySubString = $this->generateQueryString("manufacturer");
		if (!empty($querySubString)){
			$query->andWhere($querySubString);
		}

		$list = $query->all();

		$sumCostprice = 0;
		$sumSellingprice = 0;
		foreach ($list as  $value) {
			$sumCostprice += $value['COSTPRICE'];
			$sumSellingprice += $value['SELLINGPRICE'];
		}
		return $this->render("index",['list' => $list,'sumCostprice'=>$sumCostprice,'sumSellingprice'=>$sumSellingprice]);
	}

	public function actionListbyid()
	{
		$productid = $_GET['id'];

		$product = array();

		if (!empty($productid)){
			$query = new \yii\db\Query();
			$product = $query->select("*")
					->from("JXC_PRODUCT")
					->where('productid = '.$productid)->one();
		}

		return $this->render("listbyid",['product'=>$product]);
	}

	public function actionDetail()
	{
		$productid = $_GET['id'];

		$product = array();

		if (!empty($productid)){
			$query = new \yii\db\Query();
			$product = $query->select("*")
					->from("JXC_PRODUCT")
					->where('productid ='.$productid)->one();
		}

		$retArr = array();
		$retArr[] = ['label' => '���' , 'value' => $product['PRODUCTID']];
		$retArr[] = ['label' => '��Ʒ����' , 'value' => $product['PRODUCTNAME']];
		$retArr[] = ['label' => '��Ʒͼ��' , 'value' => $product['DRAWINGNO']];
		$retArr[] = ['label' => '����ͺ�' , 'value' => $product['SPECIFICATION']];
		$retArr[] = ['label' => 'ʹ�ò�λ' , 'value' => $product['USEPOSITION']];
		$retArr[] = ['label' => '��������' , 'value' => $product['SHIPMODEL']];
		$retArr[] = ['label' => '������׼' , 'value' => $product['MAKESTANDARD']];
		$retArr[] = ['label' => '��������' , 'value' => $product['MANUFACTURER']];
		$retArr[] = ['label' => '��������' , 'value' => $product['CHARACTERISTIC']];
		$retArr[] = ['label' => 'ʹ�����' , 'value' => $product['USESTATUS']];
		$retArr[] = ['label' => 'ʹ������' , 'value' => $product['USEAGELIMIT']];
		$retArr[] = ['label' => 'ʹ�÷�Χ' , 'value' => $product['USESCOPE']];
		$retArr[] = ['label' => '��ŷ���' , 'value' => $product['PRESERVE']];
		$retArr[] = ['label' => '�ο��ɱ���' , 'value' => $product['COSTPRICE']];
		$retArr[] = ['label' => '�ο����ۼ�' , 'value' => $product['SELLINGPRICE']];

		return json_encode($retArr);
	}
}